package com.linkhub.oms.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.github.yulichang.annotation.EntityMapping;
import com.linkhub.oms.common.enums.mp.*;
import com.linkhub.oms.entity.innerproperties.OrderFulfillmentEntity;
import com.linkhub.oms.entity.typehandler.QtFastjsonTypeHandler;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.FieldNameConstants;

import javax.persistence.Transient;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 订单表
 * </p>
 *
 * @author echo
 * @since 2024-03-28
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName(value = "t_order", autoResultMap = true)
@ApiModel(value = "OrderEntity对象", description = "订单表")
@FieldNameConstants
public class OrderEntity implements Serializable {

    private static final String SPLIT_SOURCE_ORDER_FLAG = "0";


    @ApiModelProperty(value = "主键")
    @TableId(value = "Fid", type = IdType.AUTO)
    private Long id;

    @ApiModelProperty(value = "系统商店id")
    @TableField("Fsys_shop_id")
    private String sysShopId;

    @ApiModelProperty(value = "系统orderId")
    @TableField("Fsys_order_id")
    private String sysOrderId;

    @ApiModelProperty(value = "平台订单id")
    @TableField("Fplat_form_order_id")
    private Long platFormOrderId;

    @ApiModelProperty(value = "平台订单号")
    @TableField("Forder_number")
    private String orderNumber;

    @ApiModelProperty(value = "用户邮箱")
    @TableField("Femail")
    private String email;

    @ApiModelProperty(value = "订单总金额")
    @TableField("Fcurrent_total_price")
    private String currentTotalPrice;

    @ApiModelProperty(value = "付款状态")
    @TableField("Ffinancial_status")
    private String financialStatus;

    @ApiModelProperty(value = "发货状态")
    @TableField("Ffulfillment_status")
    private String fulfillmentStatus;

    @ApiModelProperty(value = "买家备注")
    @TableField("Fbuyer_note")
    private String buyerNote;

    @ApiModelProperty(value = "卖家备注")
    @TableField("Fseller_note")
    private String sellerNote;

    @ApiModelProperty(value = "平台订单物流信息，存储json串")
    @TableField(value = "Ffulfillments", typeHandler = QtFastjsonTypeHandler.class)
    private List<OrderFulfillmentEntity> fulfillments;

    @ApiModelProperty(value = "员工拒审原因")
    @TableField("Frefusal_reason")
    private String refusalReason;

    @ApiModelProperty(value = "买家指定配送方式")
    @TableField("Fshipping_method")
    private String shippingMethod;

    @ApiModelProperty(value = "渠道编码")
    @TableField("Fchannel_code")
    private String channelCode;

    @ApiModelProperty(value = "云途物流单号")
    @TableField("Fway_bill_number")
    private String wayBillNumber;

    @ApiModelProperty(value = "跟踪号")
    @TableField("Ftracking_number")
    private String trackingNumber;

    @ApiModelProperty(value = "物流面单pdf链接")
    @TableField("Fway_bill_pdf_file")
    private String wayBillPdfFile;

    @ApiModelProperty(value = "仓库")
    @TableField("Fstash")
    private String stash;

    @ApiModelProperty(value = "预估运费")
    @TableField("Festimated_freight")
    private BigDecimal estimatedFreight;

    @ApiModelProperty(value = "对外挂号费")
    @TableField("Fregistration_fee")
    private BigDecimal registrationFreight;

    @ApiModelProperty(value = "币种")
    @TableField("Fcurrency")
    private String currency;

    @ApiModelProperty(value = "VAT增值税")
    @TableField("Fvat")
    private BigDecimal vat;

    @ApiModelProperty(value = "shipo手续费")
    @TableField("Fhandling_fee")
    private BigDecimal handlingFee;

    @ApiModelProperty(value = "装箱单pdf")
    @TableField("Fpackage_pdf_url")
    private String packagePdfUrl;

    @ApiModelProperty(value = "下单时间")
    @TableField("Fcreated_at")
    private String createdAt;

    @ApiModelProperty(value = "下单时间转换为标准时间")
    @TableField("Fcreated_at_convert")
    private LocalDateTime createdAtConvert;

    @ApiModelProperty(value = "付款时间")
    @TableField("Fprocessed_at")
    private String processedAt;

    @ApiModelProperty(value = "订单更新时间")
    @TableField("Fupdated_at")
    private String updatedAt;

    @ApiModelProperty(value = "订单更新时间转换为标准时间")
    @TableField("Fupdated_at_convert")
    private LocalDateTime updatedAtConvert;

    @ApiModelProperty(value = "取消订单时间")
    @TableField("Fcancelled_at")
    private String cancelledAt;

    @ApiModelProperty(value = "提交履约时间")
    @TableField("Ffulfillment_time")
    private LocalDateTime fulfillmentTime;

    @ApiModelProperty(value = "发货时间")
    @TableField("Fdelivery_time")
    private LocalDateTime deliveryTime;

    @ApiModelProperty(value = "订单状态：1-未付款;2-已付款;3-部分退款;4-已退款;5-已取消；20-shipO审核中;21-待打包；22-已出库；30-已驳回;40-已发货;41-已签收；")
    @TableField("Forder_status")
    private OrderStatusEnum orderStatus;

    @ApiModelProperty(value = "客户业务状态：1-待匹配;2-待付款;3-待审核;4-待处理;5-已取消;6-非ShipO订单; 20-履约中; 30-异常；40-追踪中；")
    @TableField("Fcustomer_business_status")
    private CustomerBusinessStatusEnum customerBusinessStatus;

    @ApiModelProperty(value = "客户订单状态：0-无状态;21-有货；22-无货；")
    @TableField("Fcustomer_order_status")
    private CustomerOrderStatusEnum customerOrderStatus;

    @ApiModelProperty(value = "员工业务状态：0-客户未履约;50-待审核；55-待预报；60-待打印；65-待推送；70-待打包；75-已出库；80-追踪中；85-已取消；90-异常")
    @TableField("Femployee_business_status")
    private EmployeeBusinessStatusEnum employeeBusinessStatus;

    @ApiModelProperty(value = "员工订单状态：0-无状态;50-有货；55-无货；60-待预报；65-预报失败;70-待打印；71-打印失败；75-待推送；76-推送失败；80-待打包；85-已出库；90-已发货；95-已签收")
    @TableField("Femployee_order_status")
    private EmployeeOrderStatusEnum employeeOrderStatus;

    @ApiModelProperty(value = "shopify是否已发货标识：0-否;1-是;")
    @TableField("Fshopify_fulfillment_flag")
    private BoolEnum shopifyFulfillmentFlag;

    @ApiModelProperty(value = "运单追踪状态：101-未查到;102-待取件;103-运输中;104-到达待取;105-派件中;106-投递失败;107-已签收;108-可能异常;109-运输过久;")
    @TableField("Ftrack_business_status")
    private TrackBusinessStatusEnum trackBusinessStatus;

    @ApiModelProperty(value = "订单商品库存标识：-1-无状态；0-缺货;1-有货;  与customerOrderStatus区别为此字段标记订单有货无货状态，customerOrderStatus为商家查看列表时筛选用，方便以后拓展其他状态")
    @TableField("Forder_stock_status")
    private OrderStockStatusEnum orderStockStatus;

    @ApiModelProperty(value = "拆分标识：0-否;1-是;")
    @TableField("Fsplit_flag")
    private BoolEnum splitFlag;

    @ApiModelProperty(value = "拆分订单的原订单id")
    @TableField("Fsource_order_number")
    private String sourceOrderNumber;

    @ApiModelProperty(value = "本地化：0-否;1-是;")
    @TableField("Flocalization")
    private BoolEnum localization;

    @ApiModelProperty(value = "员工订单失败原因——外部系统返回")
    @TableField("Ffail_reason")
    private String failReason;

    @ApiModelProperty(value = "数据状态：1-有效；0-无效")
    @TableField("Fdata_status")
    private DataStatusEnum dataStatus;

    @ApiModelProperty(value = "创建时间")
    @TableField("Fcreated_time")
    private LocalDateTime createdTime;

    @ApiModelProperty(value = "修改时间")
    @TableField("Fupdate_time")
    private LocalDateTime updateTime;


    @ApiModelProperty(value = "订单行,用于接收shopify同步的数据")
    @TableField(exist = false)
    @Transient
    @EntityMapping(thisField = Fields.sysOrderId, joinField = OrderProductEntity.Fields.sysOrderId)
    private List<OrderProductEntity> orderLineItems;

    @ApiModelProperty(value = "订单收货地址")
    @TableField(exist = false)
    @Transient
    @EntityMapping(thisField = Fields.sysOrderId, joinField = OrderAddressEntity.Fields.sysOrderId)
    private OrderAddressEntity orderAddress;


    @ApiModelProperty(value = "订单商店信息")
    @TableField(exist = false)
    @EntityMapping(thisField = Fields.sysShopId, joinField = ShopEntity.Fields.sysShopId)
    private ShopEntity shopInfo;


    @ApiModelProperty(value = "订单数量")
    @TableField(exist = false)
    private Integer count;

    @ApiModelProperty(value = "渠道英文名称")
    @TableField(exist = false)
    private String channelEName;


    /**
     * 不可更新标识
     *
     * @return bool
     */
    @JSONField(serialize = false, deserialize = false)
    public boolean unRenewable() {

        return DataStatusEnum.DELETE.equals(this.getDataStatus())
                || Objects.equals(this.getLocalization(), BoolEnum.YES)
                || Objects.equals(this.getSplitFlag(), BoolEnum.YES)
                || Objects.equals(this.getCustomerBusinessStatus(), CustomerBusinessStatusEnum.VOID);

    }

    @JSONField(serialize = false, deserialize = false)
    public boolean parentSplitOrder() {
        return Objects.equals(this.getSplitFlag(), BoolEnum.YES)
                && Objects.equals(this.getSourceOrderNumber(), SPLIT_SOURCE_ORDER_FLAG);
    }

    @JSONField(serialize = false, deserialize = false)
    public boolean subSplitOrder() {
        return Objects.equals(this.getSplitFlag(), BoolEnum.YES)
                && !Objects.equals(this.getSourceOrderNumber(), SPLIT_SOURCE_ORDER_FLAG);
    }
}
